package com.ssy.lingxi.logistics.jdbc;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.logistics.entity.QCompany;
import com.ssy.lingxi.logistics.entity.QLogisticsOrder;
import com.ssy.lingxi.logistics.model.request.LogisticsHandleOrderListRequest;
import com.ssy.lingxi.logistics.model.request.LogisticsPlatformOrderListRequest;
import com.ssy.lingxi.logistics.model.request.LogisticsSubmitOrderListRequest;
import com.ssy.lingxi.logistics.model.response.LogisticsOrderHandleListResponse;
import com.ssy.lingxi.logistics.model.response.LogisticsOrderSubmitListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 物流单分页查询
 * @author yzc
 * @version 2.0.0
 * @date 2021/4/8
 */
@Slf4j
@Component
public class LogisticsOrderQueryHelper {

    @Resource
    private JPAQueryFactory jpaQueryFactory;

    public PageData<LogisticsOrderSubmitListResponse> pageSubmitLogisticsOrder(UserLoginCacheDTO userLoginCacheDTO, LogisticsSubmitOrderListRequest request, List<Integer> statusList) {
        // 查询表实体
        QLogisticsOrder logisticsOrder = QLogisticsOrder.logisticsOrder;
        QCompany company = QCompany.company;

        // 查询条件构造
        BooleanBuilder predicates = new BooleanBuilder();
        predicates.and(logisticsOrder.memberId.eq(userLoginCacheDTO.getMemberId()));
        predicates.and(logisticsOrder.roleId.eq(userLoginCacheDTO.getMemberRoleId()));

        if (!CollectionUtils.isEmpty(statusList)) {
            predicates.and(logisticsOrder.status.in(statusList));
        }
        if (Objects.nonNull(request.getStatus())) {
            predicates.and(logisticsOrder.status.eq(request.getStatus()));
        }
        if (StringUtils.hasLength(request.getLogisticsOrderNo())) {
            predicates.and(logisticsOrder.logisticsOrderNo.like("%" + request.getLogisticsOrderNo() + "%"));
        }
        if (StringUtils.hasLength(request.getRelevanceOrderCode())) {
            predicates.and(logisticsOrder.relevanceOrderCode.like("%" + request.getRelevanceOrderCode() + "%"));
        }
        if (Objects.nonNull(request.getCompanyId())) {
            predicates.and(company.id.eq(request.getCompanyId()));
        }
        if (Objects.nonNull(request.getInvoicesTimeStart())) {
            predicates.and(logisticsOrder.createTime.goe(request.getInvoicesTimeStart()));
        }
        if (Objects.nonNull(request.getInvoicesTimeEnd())) {
            predicates.and(logisticsOrder.createTime.loe(request.getInvoicesTimeEnd()));
        }

        // 关联查询
        JPAQuery<LogisticsOrderSubmitListResponse> query = jpaQueryFactory
                .select(Projections.bean(LogisticsOrderSubmitListResponse.class,
                        logisticsOrder.id,
                        logisticsOrder.companyId,
                        logisticsOrder.logisticsOrderNo,
                        logisticsOrder.relevanceOrderCode,
                        company.name.as("companyName"),
                        logisticsOrder.memberName.as("shipperMemberName"),
                        logisticsOrder.receiverMemberName.as("receiverMemberName"),
                        logisticsOrder.status,
                        logisticsOrder.createType,
                        logisticsOrder.totalCarton,
                        logisticsOrder.totalVolume,
                        logisticsOrder.totalWeight,
                        logisticsOrder.createTime.as("invoicesTime"))
                )
                .from(logisticsOrder)
                .leftJoin(company)
                .on(logisticsOrder.companyId.eq(company.id))
                .where(predicates);

        long totalCount = query.fetchCount();
        List<LogisticsOrderSubmitListResponse> responses = query.orderBy(logisticsOrder.createTime.desc())
                .limit(request.getPageSize())
                .offset(request.getPageSize() * (request.getCurrent() -1))
                .fetch();

        return new PageData<>(totalCount, responses);
    }

    public PageData<LogisticsOrderHandleListResponse> pageHandleLogisticsOrder(UserLoginCacheDTO userLoginCacheDTO, LogisticsHandleOrderListRequest request, Long memberId) {
        // 查询表实体
        QLogisticsOrder logisticsOrder = QLogisticsOrder.logisticsOrder;
        QCompany company = QCompany.company;

        // 查询条件构造
        BooleanBuilder predicates = new BooleanBuilder();
        predicates.and(company.companyMemberId.eq(userLoginCacheDTO.getMemberId()));
        predicates.and(company.companyRoleId.eq(userLoginCacheDTO.getMemberRoleId()));

        if (Objects.nonNull(request.getStatus())) {
            predicates.and(logisticsOrder.status.eq(request.getStatus()));
        }
        if (StringUtils.hasLength(request.getLogisticsOrderNo())) {
            predicates.and(logisticsOrder.logisticsOrderNo.like("%" + request.getLogisticsOrderNo() + "%"));
        }
        if (StringUtils.hasLength(request.getShipperMemberName())) {
            predicates.and(logisticsOrder.memberName.like("%" + request.getShipperMemberName() + "%"));
        }
        if (Objects.nonNull(request.getInvoicesTimeStart())) {
            predicates.and(logisticsOrder.createTime.goe(request.getInvoicesTimeStart()));
        }
        if (Objects.nonNull(request.getInvoicesTimeEnd())) {
            predicates.and(logisticsOrder.createTime.loe(request.getInvoicesTimeEnd()));
        }

        // 关联查询
        JPAQuery<LogisticsOrderHandleListResponse> query = jpaQueryFactory
                .select(Projections.bean(LogisticsOrderHandleListResponse.class,
                        logisticsOrder.id,
                        logisticsOrder.companyId,
                        logisticsOrder.logisticsOrderNo,
                        logisticsOrder.relevanceOrderCode,
                        logisticsOrder.memberName.as("shipperMemberName"),
                        logisticsOrder.receiverMemberName.as("receiverMemberName"),
                        logisticsOrder.status,
                        logisticsOrder.createType,
                        logisticsOrder.totalCarton,
                        logisticsOrder.totalVolume,
                        logisticsOrder.totalWeight,
                        logisticsOrder.createTime.as("invoicesTime"))
                )
                .from(logisticsOrder)
                .leftJoin(company)
                .on(logisticsOrder.companyId.eq(company.id))
                .where(predicates);

        long totalCount = query.fetchCount();
        List<LogisticsOrderHandleListResponse> responses = query.orderBy(logisticsOrder.createTime.desc())
                .limit(request.getPageSize())
                .offset(request.getPageSize() * (request.getCurrent() - 1))
                .fetch();

        return new PageData<>(totalCount, responses);

    }

    public PageData<LogisticsOrderHandleListResponse> pagePlatformLogisticsOrder(LogisticsPlatformOrderListRequest request) {
        // 查询表实体
        QLogisticsOrder logisticsOrder = QLogisticsOrder.logisticsOrder;
        QCompany company = QCompany.company;

        // 查询条件构造
        BooleanBuilder predicates = new BooleanBuilder();

        if (Objects.nonNull(request.getStatus())) {
            predicates.and(logisticsOrder.status.eq(request.getStatus()));
        }
        if (Objects.nonNull(request.getCompanyId())) {
            predicates.and(logisticsOrder.companyId.eq(request.getCompanyId()));
        }
        if (StringUtils.hasLength(request.getLogisticsOrderNo())) {
            predicates.and(logisticsOrder.logisticsOrderNo.like("%" + request.getLogisticsOrderNo() + "%"));
        }
        if (StringUtils.hasLength(request.getRelevanceOrderCode())) {
            predicates.and(logisticsOrder.relevanceOrderCode.like("%" + request.getRelevanceOrderCode() + "%"));
        }
        if (StringUtils.hasLength(request.getShipperMemberName())) {
            predicates.and(logisticsOrder.memberName.like("%" + request.getShipperMemberName() + "%"));
        }
        if (Objects.nonNull(request.getInvoicesTimeStart())) {
            predicates.and(logisticsOrder.createTime.goe(request.getInvoicesTimeStart()));
        }
        if (Objects.nonNull(request.getInvoicesTimeEnd())) {
            predicates.and(logisticsOrder.createTime.loe(request.getInvoicesTimeEnd()));
        }

        // 关联查询
        JPAQuery<LogisticsOrderHandleListResponse> query = jpaQueryFactory
                .select(Projections.bean(LogisticsOrderHandleListResponse.class,
                        logisticsOrder.id,
                        logisticsOrder.companyId,
                        logisticsOrder.logisticsOrderNo,
                        logisticsOrder.relevanceOrderCode,
                        company.name.as("companyName"),
                        logisticsOrder.memberName.as("shipperMemberName"),
                        logisticsOrder.receiverMemberName.as("receiverMemberName"),
                        logisticsOrder.status,
                        logisticsOrder.createType,
                        logisticsOrder.totalCarton,
                        logisticsOrder.totalVolume,
                        logisticsOrder.totalWeight,
                        logisticsOrder.createTime.as("invoicesTime"))
                )
                .from(logisticsOrder)
                .leftJoin(company)
                .on(logisticsOrder.companyId.eq(company.id))
                .where(predicates);

        long totalCount = query.fetchCount();
        List<LogisticsOrderHandleListResponse> responses = query.orderBy(logisticsOrder.createTime.desc())
                .limit(request.getPageSize())
                .offset(request.getPageSize() * (request.getCurrent() -1))
                .fetch();

        return new PageData<>(totalCount, responses);
    }

}
